package huawei.widget;

import android.content.Context;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import o.fpq;
import o.fpr;
import o.fpw;

public class HwSortedTextListAdapter extends BaseAdapter {
    private static final String EMPTY_SECTION = "";
    private static final int HASH_MAP_LENGTH = 16;
    private static final int SORTING_NUM = -1;
    private Context mContext;
    private List<? extends Map<String, ?>> mData;
    private fpw mIndexer;
    private LayoutInflater mInflater;
    private boolean mIsDigitLast;
    private int mItemResource;
    private final Object mLock;
    private Map<String, String> mSectionMap;
    private int mSortKeyId;
    private Map<String, CollationKey> mSortKeyMap;
    private String mSortKeyName;

    public HwSortedTextListAdapter(Context context, int i, List<? extends Map<String, ?>> list, String str) {
        this(context, i, 0, 0, list, str, false);
    }

    public HwSortedTextListAdapter(Context context, int i, int i2, List<? extends Map<String, ?>> list, String str, boolean z) {
        this(context, i, i2, 0, list, str, z);
    }

    public HwSortedTextListAdapter(Context context, int i, int i2, int i3, List<? extends Map<String, ?>> list, String str) {
        this(context, i, i2, i3, list, str, false);
    }

    public HwSortedTextListAdapter(Context context, int i, int i2, int i3, List<? extends Map<String, ?>> list, String str, boolean z) {
        this.mSortKeyId = 0;
        this.mLock = new Object();
        this.mIsDigitLast = false;
        this.mContext = context;
        this.mInflater = (LayoutInflater) context.getSystemService("layout_inflater");
        this.mItemResource = i;
        this.mSortKeyId = i2;
        this.mIsDigitLast = z;
        this.mData = list;
        this.mSortKeyName = str;
        buildSectionIndexer();
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        return createViewFromResource(i, view, viewGroup, this.mItemResource);
    }

    private View createViewFromResource(int i, View view, ViewGroup viewGroup, int i2) {
        View inflate;
        if (view == null) {
            inflate = this.mInflater.inflate(i2, viewGroup, false);
        } else {
            inflate = view;
        }
        try {
            TextView textView;
            if (this.mSortKeyId == 0) {
                textView = (TextView) inflate;
            } else {
                textView = (TextView) inflate.findViewById(this.mSortKeyId);
            }
            Object item = getItem(i);
            if (item instanceof CharSequence) {
                textView.setText((CharSequence) item);
            } else {
                textView.setText(String.valueOf(item));
            }
            return inflate;
        } catch (ClassCastException e) {
            throw new IllegalStateException("HwSortedTextListAdapter requires the resource ID to be a TextView");
        }
    }

    public void sort(final Comparator<Object> comparator) {
        synchronized (this.mLock) {
            Collections.sort(this.mData, new Comparator<Map<String, ?>>(this) {
                final /* synthetic */ HwSortedTextListAdapter ˋ;

                public /* synthetic */ int compare(Object obj, Object obj2) {
                    return ˎ((Map) obj, (Map) obj2);
                }

                public int ˎ(Map<String, ?> map, Map<String, ?> map2) {
                    String obj = map.get(this.ˋ.mSortKeyName).toString();
                    String obj2 = map2.get(this.ˋ.mSortKeyName).toString();
                    String str = (String) this.ˋ.mSectionMap.get(obj);
                    String str2 = (String) this.ˋ.mSectionMap.get(obj2);
                    if (comparator == null) {
                        return 0;
                    }
                    if ("".equals(str) && "".equals(str2)) {
                        return comparator.compare(obj, obj2);
                    }
                    if ("".equals(str) && !"".equals(str2)) {
                        return 1;
                    }
                    if ("".equals(str2) && !"".equals(str)) {
                        return -1;
                    }
                    if (this.ˋ.mIsDigitLast && !str.equals(str2)) {
                        if ("#".equals(str)) {
                            return 1;
                        }
                        if ("#".equals(str2)) {
                            return -1;
                        }
                    }
                    if ("zh".equals(Locale.getDefault().getLanguage())) {
                        int access$300 = this.ˋ.getStrokeSort(str, str2);
                        if (access$300 != 0) {
                            return access$300;
                        }
                    }
                    int compare = comparator.compare(str, str2);
                    return compare == 0 ? comparator.compare(obj, obj2) : compare;
                }
            });
        }
        notifyDataSetChanged();
    }

    private void buildSectionIndexer() {
        int i;
        int i2 = 0;
        fpq ˎ = fpq.ˎ();
        Map linkedHashMap = new LinkedHashMap(16);
        this.mSectionMap = new HashMap(16);
        this.mSortKeyMap = new HashMap(16);
        final Comparator instance = Collator.getInstance();
        int size = this.mData.size();
        for (i = 0; i < size; i++) {
            Object ˋ;
            String obj = ((Map) this.mData.get(i)).get(this.mSortKeyName).toString();
            this.mSortKeyMap.put(obj, instance.getCollationKey(obj));
            if (VERSION.SDK_INT >= 24) {
                ˋ = TextUtils.isEmpty(obj) ? "" : ˎ.ˋ(obj);
            } else {
                String pinYinHeadChar = TextUtils.isEmpty(obj) ? "" : getPinYinHeadChar(obj.substring(0, 1));
            }
            this.mSectionMap.put(obj, ˋ);
            if (linkedHashMap.containsKey(ˋ)) {
                linkedHashMap.put(ˋ, Integer.valueOf(((Integer) linkedHashMap.get(ˋ)).intValue() + 1));
            } else {
                linkedHashMap.put(ˋ, Integer.valueOf(1));
            }
        }
        String[] strArr = (String[]) linkedHashMap.keySet().toArray(new String[0]);
        i = strArr.length;
        Arrays.sort(strArr, new Comparator<String>(this) {
            final /* synthetic */ HwSortedTextListAdapter ˏ;

            public /* synthetic */ int compare(Object obj, Object obj2) {
                return ˏ((String) obj, (String) obj2);
            }

            public int ˏ(String str, String str2) {
                if ("".equals(str)) {
                    return 1;
                }
                if ("".equals(str2)) {
                    return -1;
                }
                if (this.ˏ.mIsDigitLast) {
                    if ("#".equals(str)) {
                        return 1;
                    }
                    if ("#".equals(str2)) {
                        return -1;
                    }
                }
                if ("zh".equals(Locale.getDefault().getLanguage())) {
                    int access$300 = this.ˏ.getStrokeSort(str, str2);
                    if (access$300 != 0) {
                        return access$300;
                    }
                }
                return instance.compare(str, str2);
            }
        });
        int[] iArr = new int[i];
        while (i2 < i) {
            iArr[i2] = ((Integer) linkedHashMap.get(strArr[i2])).intValue();
            i2++;
        }
        this.mIndexer = new fpw(strArr, iArr);
        sort(instance);
    }

    private int getStrokeSort(String str, String str2) {
        if (str == null || str2 == null) {
            return 0;
        }
        int length = str.length();
        int length2 = str2.length();
        if (length > length2) {
            return 1;
        }
        if (length < length2) {
            return -1;
        }
        return 0;
    }

    private String getPinYinHeadChar(String str) {
        Object ˋ = fpr.ˏ().ˋ(str);
        return TextUtils.isEmpty(ˋ) ? "" : ˋ.substring(0, 1).toUpperCase(Locale.ENGLISH);
    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public Context getContext() {
        return this.mContext;
    }

    public LayoutInflater getInflater() {
        return this.mInflater;
    }

    public int getCount() {
        return this.mData.size();
    }

    public Object getItem(int i) {
        if (this.mData == null || i < 0 || i >= this.mData.size()) {
            return "";
        }
        return ((Map) this.mData.get(i)).get(this.mSortKeyName);
    }

    public int getPosition(Map<String, ?> map) {
        return this.mData.indexOf(map);
    }

    public long getItemId(int i) {
        return (long) i;
    }

    public void setViewImage(ImageView imageView, String str) {
        try {
            imageView.setImageResource(Integer.parseInt(str));
        } catch (NumberFormatException e) {
            imageView.setImageURI(Uri.parse(str));
        }
    }

    public void setViewImage(ImageView imageView, int i) {
        imageView.setImageResource(i);
    }

    public void setViewText(TextView textView, String str) {
        textView.setText(str);
    }

    public void setSortKeyName(String str) {
        this.mSortKeyName = str;
    }

    public String getSortKeyName() {
        return this.mSortKeyName;
    }

    public boolean isDigitLast() {
        return this.mIsDigitLast;
    }

    public Object[] getSections() {
        return this.mIndexer.getSections();
    }

    public int getPositionForSection(int i) {
        return this.mIndexer.getPositionForSection(i);
    }

    public int getSectionForPosition(int i) {
        return this.mIndexer.getSectionForPosition(i);
    }

    public Object getSectionNameForPosition(int i) {
        return this.mIndexer.getSections()[getSectionForPosition(i)];
    }
}
